# -*- coding: utf-8 -*-
# @File:     settings.py
# @Author:
# @DateTime: 2025/10/23/11:09
import json
from pathlib import Path
cursor_path = Path(__file__).resolve().parent.parent  # os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
import yaml
from app.utils.encryption import getDataAes
from typing import List


conf_path = Path.joinpath(cursor_path, "config.yaml")

with open(conf_path, 'r') as file:
    CNF_DATA = yaml.safe_load(file)

try:
    SECRET_KEY = CNF_DATA['serve']['secret']
    DEBUG = CNF_DATA['serve']['debug']
    ALLOWED_HOSTS = CNF_DATA['serve']['allow_host']

    MYSQL_HOST = CNF_DATA['mysql']['host']
    MYSQL_PORT = CNF_DATA['mysql']['port']
    MYSQL_USERNAME = CNF_DATA['mysql']['user']
    MYSQL_PASSWORD = CNF_DATA['mysql']['password']
    MYSQL_DATABASE = CNF_DATA['mysql']['database']

    REDIS_HOST = CNF_DATA['redis']['host']
    REDIS_PORT = CNF_DATA['redis']['port']
    REDIS_PASSWORD = CNF_DATA['redis']['password']
    REDIS_DB = CNF_DATA['redis']['db']

    MINIO_HOST = CNF_DATA['minio']['client_host']
    MINIO_PORT = CNF_DATA['minio']['client_port']
    MINIO_PWD = CNF_DATA['minio']['password']
    MINIO_USER = CNF_DATA['minio']['user']
    MINIO_PROXY = CNF_DATA['minio']['proxy']
    MINIO_BUCKET = CNF_DATA['minio']['bucket']

    RABBITMQ_HOST = CNF_DATA['rabbitmq']['host']
    RABBITMQ_PORT = CNF_DATA['rabbitmq']['port']
    RABBITMQ_USER = CNF_DATA['rabbitmq']['user']
    RABBITMQ_PWD = CNF_DATA['rabbitmq']['password']
    RABBITMQ_QUEUE_LIST = CNF_DATA['rabbitmq']['queue_list']
    RABBITMQ_CLIENT_KEY = CNF_DATA['rabbitmq']['client_key']

    SMTP_SERVE = CNF_DATA['smtp']['serve']
    SMTP_PORT = CNF_DATA['smtp']['port']
    SMTP_SENDER = CNF_DATA['smtp']['sender']
    SMTP_PWD = CNF_DATA['smtp']['password']

    CNF_DATA['mysql']['user'] = MYSQL_USERNAME = getDataAes(SECRET_KEY, MYSQL_USERNAME)
    CNF_DATA['mysql']['password'] = MYSQL_PASSWORD = getDataAes(SECRET_KEY, MYSQL_PASSWORD)
    CNF_DATA['redis']['password'] = REDIS_PASSWORD = getDataAes(SECRET_KEY, REDIS_PASSWORD)
    CNF_DATA['minio']['password'] = MINIO_PWD = getDataAes(SECRET_KEY, MINIO_PWD)
    CNF_DATA['minio']['user'] = MINIO_USER = getDataAes(SECRET_KEY, MINIO_USER)
    CNF_DATA['rabbitmq']['user'] = RABBITMQ_USER = getDataAes(SECRET_KEY, RABBITMQ_USER)
    CNF_DATA['rabbitmq']['password'] = RABBITMQ_PWD = getDataAes(SECRET_KEY, RABBITMQ_PWD)

    CNF_DATA['milvus']['user'] = getDataAes(SECRET_KEY, CNF_DATA['milvus']['user'])
    CNF_DATA['milvus']['password'] = getDataAes(SECRET_KEY, CNF_DATA['milvus']['password'])

except Exception as e:
    print(e)
    raise Exception('请检查配置文件，格式不正确')


if MYSQL_USERNAME and MYSQL_PASSWORD and REDIS_PASSWORD and MINIO_PWD and MINIO_USER and CNF_DATA['milvus']['user'] and CNF_DATA['milvus']['password']:
    pass
else:
    raise Exception('请检查配置文件，解密失败')


# ----------------------------------------------------------------------------------------------
# 根路径
BASE_DIR: str = str(Path(__file__).resolve().parent.parent)

API_V1_STR: str = "/v1"
# jwt加密算法
JWT_ALGORITHM: str = "HS256"
# jwt token过期时间 60 minutes * 24 hours * 8 days = 8 days
ACCESS_ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 60 * 24
# jwt token过期时间 60 minutes * 24 hours * 8 days = 8 days
ACCESS_REFRESH_TOKEN_EXPIRE_MINUTES: int = 60 * 60 * 24 * 8


# 项目信息
PROJECT_NAME: str = "API"
DESCRIPTION: str = ""
SERVER_NAME: str = "API_V1"
SERVER_HOST: str = "http://127.0.0.1:8000"
VERSION: str = "1.0.1"


# 跨域
BACKEND_CORS_ORIGINS: List[str] = ALLOWED_HOSTS


SQLALCHEMY_DATABASE_URL = f"mysql+pymysql://{MYSQL_USERNAME}:{MYSQL_PASSWORD}@" \
                          f"{MYSQL_HOST}:{MYSQL_PORT}/{MYSQL_DATABASE}?charset=utf8mb4"

REDIS_URL: str = f"redis://:{REDIS_PASSWORD}@{REDIS_HOST}:{REDIS_PORT}/{REDIS_DB}?encoding=utf-8"


DEFAULT_ROLE: List[dict] = [
        {"name": "管理员", "sort": 1, "admin": True},
    ]

DEFAULT_USER: List[dict] = [
        {"nickname": "superadmin", "username": "superadmin", "password": "superadmin", "is_superuser": 1},
        {"nickname": "admin", "username": "admin", "password": "admin", "is_superuser": 1},
    ]